import selenium.webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest,time
from selenium.webdriver.common.keys import Keys
import GreaterWMSyaml,asn


def chrome_driver():
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.maximize_window()
    return driver
class GreaterWMSInstellTest(unittest.TestCase):
    cfg = GreaterWMSyaml.ReadYaml.read_yaml()
    def __init__(self, driver, url=cfg['url']):
        self.driver = driver
        self.wait = WebDriverWait(driver=self.driver, timeout=5, poll_frequency=1.5)
        self.driver.get(url)

    def loc_wait(self, loc):
        return self.wait.until(EC.presence_of_element_located(loc))

    def loc_close(self):
        #按ESC取消业已打开的注册页面
        self.find_ele((By.XPATH,'/html/body/div[3]/div[2]/div/div[1]/button/span[2]/span/i')).click()

    def find_ele(self, loc):
        self.loc_wait(loc)
        return self.driver.find_element(*(loc))

    def find_eles(self, loc):
        self.loc_wait(loc)
        return self.driver.find_elements(*(loc))

    def set_language(self):
        self.find_ele((By.XPATH, '//i[text()="translate"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//div[text()="中文简体"]/parent::div')).click()
        time.sleep(5)

    def login(self, username=cfg['login']['username'], passwrod=cfg['login']['password']):
        self.find_ele((By.XPATH, '//span[text()="登入"]/ancestor::button')).click()
        #self.find_ele((By.XPATH, '//span[text()="管理员登入"]/ancestor::button')).click()
        self.find_ele((By.XPATH,'/html/body/div[3]/div[2]/div/div[1]/div[1]/div/div[2]/div[2]')).click()
        self.find_ele((By.XPATH, '//input[@aria-label="管理员"]')).send_keys(username)
        self.find_ele((By.XPATH, '//input[@aria-label="密码"]')).send_keys(passwrod)
        self.find_ele((By.XPATH, '//div[contains(@class,"justify-start")]//'
                                 'span[text()="登入"]/ancestor::button')).click()

    def add_company(self,companyname=cfg['baseinfo']['companyname'],
                    cityname=cfg['baseinfo']['cityname'],
                    address=cfg['baseinfo']['address'],
                    phone=cfg['baseinfo']['phone'],
                    director=cfg['baseinfo']['director']):
        self.find_ele((By.XPATH, '//div[contains(text(),"基本设置")]/parent::a')).click()
        self.find_ele((By.XPATH,'//span[@class="block" and text()="新增"]')).click()
        self.find_ele((By.XPATH, '//input[@aria-label="公司名称"]')).send_keys(companyname)
        self.find_ele((By.XPATH, '//input[@aria-label="所在城市"]')).send_keys(cityname)
        self.find_ele((By.XPATH, '//input[@aria-label="地址"]')).send_keys(address)
        self.find_ele((By.XPATH, '//input[@aria-label="联系方式"]')).send_keys(phone)
        self.find_ele((By.XPATH, '//input[@aria-label="负责人"]')).send_keys(director)
        self.find_ele((By.XPATH, '//span[text()="确认"]/ancestor::button')).click()
        time.sleep(5)

    def add_suplier(self,supliername=cfg['baseinfo']['suplier']['supliername'],
                    cityname=cfg['baseinfo']['suplier']['cityname'],
                    address=cfg['baseinfo']['suplier']['address'],
                    phone=cfg['baseinfo']['suplier']['phone'],
                    director=cfg['baseinfo']['suplier']['director'],
                    level=cfg['baseinfo']['suplier']['level']):
        self.find_ele((By.XPATH, '//div[contains(text(),"基本设置")]/parent::a')).click()
        self.find_ele((By.XPATH,'//div[text()="供应商"]/ancestor::a')).click()
        self.find_ele((By.XPATH, '//span[text()="新增"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//input[@aria-label="供应商名称"]')).send_keys(supliername)
        self.find_ele((By.XPATH, '//input[@aria-label="所在城市"]')).send_keys(cityname)
        self.find_ele((By.XPATH, '//input[@aria-label="地址"]')).send_keys(address)
        self.find_ele((By.XPATH, '//input[@aria-label="联系方式"]')).send_keys(phone)
        self.find_ele((By.XPATH, '//input[@aria-label="负责人"]')).send_keys(director)
        self.find_ele((By.XPATH, '//input[@aria-label="供应商等级"]')).send_keys(level)
        self.find_ele((By.XPATH, '//span[text()="确认"]/ancestor::button')).click()

    def add_customer(self,customername=cfg['baseinfo']['customer']['customername'],
                     cityname=cfg['baseinfo']['customer']['cityname'],
                     address=cfg['baseinfo']['customer']['address'],
                     phone=cfg['baseinfo']['customer']['phone'],
                     director=cfg['baseinfo']['customer']['director'],
                     level=cfg['baseinfo']['customer']['level']):
        self.find_ele((By.XPATH, '//div[contains(text(),"基本设置")]/parent::a')).click()
        self.find_ele((By.XPATH,'//div[text()="客户"]/ancestor::a')).click()
        self.find_ele((By.XPATH, '//span[text()="新增"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//input[@aria-label="客户名称"]')).send_keys(customername)
        self.find_ele((By.XPATH, '//input[@aria-label="所在城市"]')).send_keys(cityname)
        self.find_ele((By.XPATH, '//input[@aria-label="地址"]')).send_keys(address)
        self.find_ele((By.XPATH, '//input[@aria-label="联系方式"]')).send_keys(phone)
        self.find_ele((By.XPATH, '//input[@aria-label="负责人"]')).send_keys(director)
        self.find_ele((By.XPATH, '//input[@aria-label="客户等级"]')).send_keys(level)
        self.find_ele((By.XPATH, '//span[text()="确认"]/ancestor::button')).click()

class TestInitGreaterWMS(unittest.TestCase):

    def setUp(self):
        self.driver = chrome_driver()

    def test_asn_001(self):
        page = GreaterWMSInstellTest(self.driver)
        page.loc_close()
        page.set_language()
        page.loc_close()
        page.login()
        #page.add_company()
        #page.add_suplier()
        page.add_customer()
        time.sleep(5)


    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()